package com.ycp.erp.login.activity;

import android.Manifest;
import android.annotation.SuppressLint;
import android.content.pm.PackageManager;
import android.os.Handler;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.viewpager.widget.ViewPager;
import butterknife.BindView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.one.common.config.CMemoryData;
import com.one.common.config.CPersisData;
import com.one.common.config.RouterPath;
import com.one.common.manager.RouterManager;
import com.one.common.utils.Logger;
import com.one.common.utils.Toaster;
import com.one.common.view.base.BaseActivity;
import com.one.common.view.viewpagetitle.MyPagerAdapter;
import com.tbruyelle.rxpermissions2.RxPermissions;
import com.ycp.erp.R;
import com.ycp.erp.login.presenter.LoginPresenter;
import com.ycp.erp.login.view.SendCodeView;

import java.util.ArrayList;


import static android.Manifest.permission.ACCESS_COARSE_LOCATION;
import static android.Manifest.permission.ACCESS_FINE_LOCATION;
import static android.Manifest.permission.CAMERA;
import static android.Manifest.permission.READ_EXTERNAL_STORAGE;
import static android.Manifest.permission.READ_PHONE_STATE;
import static android.Manifest.permission.WRITE_EXTERNAL_STORAGE;

/**
 * creator: zhulunjun
 * time:    2018/8/16
 * describe: loading页
 */
@Route(path = RouterPath.LOADING)
public class LoadingActivity extends BaseActivity<LoginPresenter> implements SendCodeView {
//
    @BindView(R.id.iv_logo)
    ImageView ivLogo;
    @BindView(R.id.v_line)
    View vLine;
    @BindView(R.id.vp_loading)
    ViewPager vpLoading;
    @BindView(R.id.tv_go)
    TextView tvGo;
    @BindView(R.id.rl_first)
    RelativeLayout rlFirst;

    @Override
    protected void initPresenter() {
        mPresenter = new LoginPresenter(this, this);
    }

    @Override
    public int getLayoutResId() {
        return R.layout.activity_loading;
    }

    @Override
    protected void initTitle() {
        super.initTitle();
        noHasTitle();
    }

    @Override
    public void initView() {
        super.initView();
        if (CPersisData.getIsFirst() && !CMemoryData.isDriver()) {
            // 第一次进入
            rlFirst.setVisibility(View.VISIBLE);
            vpLoading.setAdapter(new MyPagerAdapter(getViewArray()));
            tvGo.setOnClickListener(v -> requestPermission());
            vpLoading.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                @Override
                public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

                }

                @Override
                public void onPageSelected(int position) {
                    if(position == 2){
                        tvGo.setVisibility(View.VISIBLE);
                    } else {
                        tvGo.setVisibility(View.GONE);
                    }
                }

                @Override
                public void onPageScrollStateChanged(int state) {

                }
            });
        } else {
            requestPermission();
        }
    }

    @Override
    public void initData() {
        super.initData();
        CPersisData.setJpushId(this);
        if (CPersisData.getIsLogin()) {
//            mPresenter.getUserState();
        }
    }


    @SuppressLint("CheckResult")
    private void requestPermission() {
//        if (android.os.Build.VERSION.SDK_INT >= 29) {
//            if (checkPermission()){
//                if (CPersisData.getIsFirst()) {
//                    CPersisData.setIsFirst(false);
//                    goMain();
//                } else {
//                    new Handler().postDelayed(this::goMain, 2000);
//                }
//            }else {
//                // 未获取权限
//                Toaster.showShortToast("您没有授权权限，请在设置中打开授权");
//                finishPage();
//            }
//        }else {
            RxPermissions rxPermissions = new RxPermissions(this);
            rxPermissions.request(
                    ACCESS_FINE_LOCATION,
                    ACCESS_COARSE_LOCATION,
                    CAMERA,
                    WRITE_EXTERNAL_STORAGE,
                    READ_PHONE_STATE,
                    READ_EXTERNAL_STORAGE)
                    .subscribe(aBoolean -> {
                        if (aBoolean) { // 在android 6.0之前会默认返回true
                            if (CPersisData.getIsFirst()) {
                                CPersisData.setIsFirst(false);
                                goMain();
                            } else {
                                new Handler().postDelayed(this::goMain, 2000);
                            }
                        } else {
                            // 未获取权限
                            Toaster.showShortToast("您没有授权权限，请在设置中打开授权");
                            finishPage();
                        }
                    });
//        }
    }

    private void goMain() {

        Logger.d("是否需要登录 "+ CPersisData.getIsLogin()+" "+ CMemoryData.isCar()+" "+ CPersisData.getIsFirst());

        if (CPersisData.getIsLogin()) {
            RouterManager.getInstance().go(LoadingActivity.this, RouterPath.MAIN_PAGE);
            close();
        } else {
            RouterManager.getInstance().go(LoadingActivity.this, RouterPath.LOGIN_PAGE);
            close();
        }
    }

    private void close(){
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                finishPage();
            }
        }, 1000);
    }

    @Override
    public void startCountDown() {

    }

    @Override
    public void resetCountDown() {

    }

    /**
     * 获取一个view数组
     */
    private ArrayList<View> getViewArray() {
        ArrayList<View> views = new ArrayList<>();
        if (getImageRes() != null) {
            for (int i = 0; i < 3; i++) {
                ImageView view = new ImageView(this);
                view.setId(i + 99);
                view.setImageResource(getImageRes()[i]);
                view.setScaleType(ImageView.ScaleType.CENTER_CROP);
                views.add(view);
            }
        }
        return views;
    }

    public int[] getImageRes() {
        return null;
    }


    public Boolean checkPermission() {
        boolean isGranted = true;
        if (android.os.Build.VERSION.SDK_INT >= 23) {
            if (this.checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
                //如果没有写sd卡权限
                isGranted = false;
            }
            if (this.checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
                isGranted = false;
            }
            if (!isGranted) {
                this.requestPermissions(
                        new String[]{Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission
                                .ACCESS_FINE_LOCATION,
                                Manifest.permission.READ_EXTERNAL_STORAGE,
                                Manifest.permission.WRITE_EXTERNAL_STORAGE,
                        CAMERA,READ_PHONE_STATE,},
                        102);
            }

        }
        return isGranted;
    }

}
